<template>
  <div>
    <div class="toolbar">
      <el-form :inline="true" class="demo-form-inline">
        <el-form-item>
          <el-input placeholder="店铺类别" v-model="searchShopTypeName"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            @click="$store.dispatch('getshopTypeList',{shopTypeName:searchShopTypeName})"
          >查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="success" @click="shopTypeVisible=true">添加店铺类别</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-table
      v-loading="$store.state.loading"
      :data="$store.state.shop.shopTypeList"
      style="width: 100%"
    >
      <el-table-column label="店铺类别图片" width="180">
        <template slot-scope="scope">
          <a :href="scope.row.shopTypePic | imgUrl" :title="scope.row.shopTypeName" target="_blank">
            <img height="80px" :src="scope.row.shopTypePic | imgUrl" alt />
          </a>
        </template>
      </el-table-column>
      <el-table-column label="店铺添加时间" width="220">
        <template slot-scope="scope">
          <i class="el-icon-time"></i>
          <span style="margin-left: 10px">{{ scope.row.createTime | date}}</span>
        </template>
      </el-table-column>
      <el-table-column label="店铺类别名称" width="180">
        <template slot-scope="scope">{{scope.row.shopTypeName}}</template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" @click="addshop(scope.row._id)">添加店铺</el-button>
          <el-button size="mini" type="danger" @click="deleteTypeList(scope.row._id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="添加店铺类别" :visible.sync="shopTypeVisible">
      <el-form>
        <el-form-item label="店铺类别名称">
          <el-input v-model="shopTypeForm.shopTypeName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="店铺类别图片">
          <el-upload
            name="shopTypePic"
            :auto-upload="false"
            :on-success="success"
            :data="shopTypeForm"
            ref="upload"
            :headers="{headerToken:$store.state.admin.token}"
            :limit="1"
            class="upload-demo"
            action="/elm/shopTypeList"
            list-type="picture"
          >
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="shopVisible = false">取 消</el-button>
        <el-button type="primary" @click="$refs.upload.submit()">确 定</el-button>
      </div>
    </el-dialog>
    <ShopDialog v-if="shopVisible" :shopVisible.sync="shopVisible" :shopTypeId="shopTypeId"></ShopDialog>
    <PageInfo action-name="getshopTypeList" :params="{shopTypeName:searchShopTypeName}"></PageInfo>
  </div>
</template>

<script>
import ShopDialog from "@/components/dialog/ShopDialog";

export default {
  name: "shop-type-list",
  components: {
    ShopDialog
  },
  data() {
    return {
      searchShopTypeName: "",
      shopTypeVisible: false,
      shopVisible: false,
      shopTypeId: "",
      shopTypeForm: {
        shopTypeName: ""
      }
    };
  },
  methods: {
    addshop(shopTypeId) {
      this.shopVisible = true;
      this.shopTypeId = shopTypeId;
    },

    //上传表单内容成功后调用的方法
    success(data) {
      if (data.ok === 1) {
        //调用getshopTypeList方法,重新获取列表数据
        this.$store.dispatch("getshopTypeList");
        //将提交框隐藏
        this.shopTypeVisible = false;
        //添加成功过给出提示
        this.$message({
          message: "添加成功",
          type: "success"
        });
        //清除表单中长传成功的图片信息
        this.$refs.upload.clearFiles();
        //清除表单中输入框的内容
        this.shopTypeForm.shopTypeName = "";
      } else {
        this.$message({
          showClose: true,
          message: data.msg,
          type: "error"
        });
      }
    },
    //删除店铺类型记录
    deleteTypeList(id) {
      this.$store.dispatch("deleteshopTypeList", id).then(res => {
        if (res.ok === 1) {
          this.$store.dispatch("getshopTypeList", {
            shopTypeName: this.searchShopTypeName,
            pageIndex: this.$store.state.pageIndex
          });
          this.$message({
            message: res.msg,
            type: "success"
          });
        } else {
          this.$message({
            showClose: true,
            message: res.msg,
            type: "error"
          });
        }
      });
    }
  }
};
</script>